import 'dart:ui';

import 'package:sprintf/sprintf.dart';

class AppUtil {
  AppUtil._init();

  static DateTime lastMonthFirstDay() {
    DateTime dateTime = DateTime.now();
    return DateTime(dateTime.year, dateTime.month - 1, 1);
  }

  static DateTime nextMonthLastDay() {
    DateTime dateTime = DateTime.now();
    DateTime nextNextTime = DateTime(dateTime.year, dateTime.month + 2, 1);
    return nextNextTime.add(const Duration(days: -1));
  }

  static String convertEpochToMonthDay(int epoch) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(epoch * 1000);
    return sprintf("%02d-%02d", [dateTime.month, dateTime.day]);
  }

  static String timeStringFromTime(int epoch) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(epoch * 1000);
    return "${dateTime.year}.${dateTime.month}.${dateTime.day}";
  }

  static String convertWan(int num, {bool hasDecimal = true}) {
    if (num > 9999 && num < 100000000) {
      double spNum = num / 10000.0;
      if (hasDecimal) {
        return sprintf("%.1f万", [spNum]);
      } else {
        return sprintf("%.0f万", [spNum]);
      }
    } else if (num >= 100000000) {
      double spNum = num / 100000000.0;
      if (hasDecimal) {
        return sprintf("%.1f亿", [spNum]);
      } else {
        return sprintf("%.0f亿", [spNum]);
      }
    }
    return "$num";
  }

  static convertHMS(int second) {
    int hour = second ~/ 3600;
    int minute = second % 3600 ~/ 60;
    int seconds = second % 3600 % 60 % 60;
    if (hour == 0) {
      return "$minute:$seconds";
    } else {
      return "$hour:$minute:$seconds";
    }
  }

  static Color hexToColor(String hexString, {String alphaChannel = 'FF'}) {
    return Color(int.parse(hexString.replaceFirst('#', '0x$alphaChannel')));
  }
}
